#include "player.h"

int leftScrolled = 0;
background *thebBg;

player::player(ALLEGRO_BITMAP *pImg)
{
	playerImg = pImg;
	player::tileSize =50;
	box.x = 80; // player starting x position
	box.y = 0;
	box.w = player::tileSize;
	box.h = player::tileSize;

	camera.x = 80; // player starting x position
	camera.y = 0;
	camera.w = player::tileSize;
	camera.h = player::tileSize;

	player::velX = 0;
	player::dirX = 0;
	player::velCamX = 0;


}


player::~player(void)
{
}

void player::Init(ALLEGRO_BITMAP *pImg)
{
}


void player::Update()
{
	player::box.x += player::velX * player::dirX;
	//player::coord.y += player::velY * player::dirY;
	player::camera.x += player::velCamX * player::dirX;

}

void player::Render()
{
	al_draw_filled_rectangle(player::box.x, player::box.y,player::box.x + player::box.w ,player::box.y + player::box.h, al_map_rgba(255,0,255,100));
		//al_draw_filled_rectangle(200, 100,tileSize,tileSize, al_map_rgba(255,0,255,100));

}

void player::SetXvel(int vel)
{
	velX = vel;
}

void player::SetCamXvel(int cVel)
{
	velCamX = cVel;
}


void player::MoveLeft()
{
	player::dirX = -1;
	player::SetCamXvel(4);


	// if player is in the most right side of scenario limit and wants to walk left untill the mmiddle of screen
	// because we are skippn n x camera vel to scroll left we have to include the velCamX to condition
	if(player::GetCamera().x - player::velCamX > (900 - WIDTH/2) - box.w)
	{
		//std::cout<<"limit right <left> border"<<900 - WIDTH/2<<std::endl;
		player::SetXvel(4);
	}
	// because we are skippn n x camera vel to scroll left we have to include the velCamX to condition
	else if (player::GetCamera().x - player::velCamX > WIDTH/2 - box.w)
	{
		player::SetXvel(0);
		//player::coord.x--;
		player::coord.x = player::coord.x - player::velCamX;

	}
	// if player is in the most left side of scenario limit and wants to walk right untill the mmiddle of screen
	// because we are skipping n x camera vel to scroll left we have to include the velCamX to condition
	else
	{
			player::SetXvel(4);

	}

	
	// border limit

	//player::camera.x--;

	//if(player::camera.x < 0)
	//{
		//std::cout<<"Reached left border limit"<<std::endl;
		//player::camera.x = 0;
	//}
	//else
	//{
	//}

}

void player::MoveRight()
{
	player::dirX =1;
	player::SetCamXvel(4);

	// player will move from most left part (scenario beginning)) untill the midle of screen
	if (player::GetRect().x < WIDTH/2 - box.w) // middle of screen minus the frame width to center it -> 640/2 - 50 = 270
	{
		player::SetXvel(4);
	}
	// if player is in the most right side of scenario limit and wants to walk left untill the mmiddle of screen
	// because we are skippn n x camera vel to scroll left we have to include the velCamX to condition
	else if(player::GetCamera().x > (900 - WIDTH/2) - box.w)
	{
		//std::cout<<"limit right border"<<900 - WIDTH/2<<std::endl;
		player::SetXvel(4);
	}
	// in the middle the player will scroll right
	else
	{
		//if (player::GetCamera().x > (900 - WIDTH/2 - box.w))
		//{
		//	player::SetXvel(4);
		//}
		//else
		//{
			player::SetXvel(0);
			//player::coord.x++;
			player::coord.x = player::coord.x + player::velCamX;
		//}
	}

	

	//player::camera.x++;

	//if(player::camera.x < 0)
	//{
	//	player::camera.x = 0;
	//}
	//else
	//{
	//}

}

void player::ResetAnimation()
{
	player::dirX = 0;
}

